package nl.project.data.controller;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.EntityNotFoundException;

import nl.project.data.Parent;
import nl.project.data.Student;
import nl.project.data.controller.MembersDAO;

public class InfoGoogleMaps extends HttpServlet {
	private static String[][] store_locations = new String[10][];
	int store_location_counter = 0;

	// GOogle maps --> eerst substring, op jaar en daarna op klas.
	// Lukt het niet, dan array in array.
	// http://googlemapsapi.blogspot.com/2007/04/v277-gmarker-show-hide-and-ishidden.html
	// Format: Jaarlaag, Niveau, Klasnr.

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		try {
//			HttpSession a = req.getSession();
//			a.setAttribute("GoogleMapsInfo", null);
			String[][] googleMapsInfoFinal;
			googleMapsInfoFinal = testRegex();
			getServletContext().setAttribute("GoogleMaps","Done");
//			a.setAttribute("GoogleMapsInfo", googleMapsInfoFinal);
			getServletContext().setAttribute("GoogleMapsInfo", googleMapsInfoFinal);
			doPost(req, resp);
		} catch (ServletException e) {
			// return something like:
			// "Something went wrong! If problem stays, contact admin!"
		}

	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.sendRedirect("/dashboard");
	}

	public String[][] testRegex() {
		//
		// Get the available classCodes
		//
		ArrayList<Student> students = new ArrayList<Student>();
		students = MembersDAO.getAllStudents();

		for (Student student : students) {
			if (store_location_counter == (store_locations.length - 1)) {
				extendArray();
			}

			
			String[] locationsArr = new String[6];
			/**
			 * contact is a String array where [0] is the classCode, [1] is the
			 * lattitude, [2] is the longitude, [3] is the full name [4] is the
			 * address
			 */
			
			try {
				int parentID = student.getStudentParentId();
				Parent parent = new Parent(parentID); //
				
				locationsArr[0] = student.getClassCode();
				locationsArr[1] = parent.getLattitude(); //locationsArr[1] = "lattitude";
				locationsArr[2] = parent.getLongitude();//locationsArr[2] = "longitude"; 
				locationsArr[3] = student.getName();
				locationsArr[4] = parent.getAddress();// only fill Address if 1 is found
				locationsArr[5] = Integer.toString(student.getStudentId());
				System.out.println(Integer.toString(student.getStudentId()));
			} catch (EntityNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			store_locations[store_location_counter] = locationsArr;
			store_location_counter++; // counter voor array index
		}
		return store_locations;

	}

	// return store_locations;
	/**
	 * Makes a temporary string array which is 3 slots bigger then original array
	 * Fills temporary array with original data.
	 * Replaces original array with new array (3 slots longer)
	 */
	private static void extendArray() {
		// create a new array 3 larger than the original
		String[][] tempArray = new String[store_locations.length + 3][];

		// copy all of the data to the new array
		for (int i = 0; i < store_locations.length; i++)
			tempArray[i] = store_locations[i];

		// since the variables are just pointers we just need to make the
		// nameArray point to our new array
		store_locations = tempArray;
	}
}
